# -*- coding: utf-8 -*-
#
# geXtras plugin for gedit
#
# See README for info or go to http://code.google.com/p/gextras/
#
# Copyright (c) 2010-2011, Simon Downes <simon@simondownes.co.uk>
#
# Licensed under the MIT License;
# A copy of the license is included in the LICENSE file accompanying this
# distribution or online at http://www.opensource.org/licenses/mit-license.php

import gtk

class BookmarkManager( object ):
	
	def __init__( self ):
		self._docs = {}

	def get_store( self, uri ):
		if uri not in self._docs:
			self._docs[uri] = BookmarkStore()
		return self._docs[uri]

class BookmarkStore( gtk.ListStore ):
	
	_iters = {}
	
	def __init__( self ):
		super(BookmarkStore, self).__init__(self)
		self.set_column_types(int, str)
		self.set_sort_func(0, self._line_sort)
		self.set_sort_column_id(0, gtk.SORT_ASCENDING)
	
	def add( self, line, source ):
		if line not in self._iters:
			self._iters[line] = self.append([line, source])
	
	def delete( self, line ):
		if line in self._iters:
			self.remove(self._iters[line])
			del(self._iters[line])
	
	def exists( self, line ):
		return line in self._iters
	
	def toggle( self, line, source ):
		if self.exists(line):
			self.delete(line)
		else:
			self.add(line, source)
	
	def update( self, line, source ):
		if line in self._iters:
			self.set_value(self._iters[line], 1, source)
	
	def _line_sort( self, model, line1, line2 ):
		
		lft = model.get_value(line1, 0)
		rgt = model.get_value(line2, 0)
		
		if lft < rgt:
			cmp = -1
		elif lft == rgt:
			cmp = 0
		else:
			cmp = 1
		
		return cmp
